package com.shujia.flink.source

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo6KafkaSource {

  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val properties: Properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")
    properties.setProperty("group.id", "test")
    //分区自动发现
    properties.setProperty("flink.partition-discovery.interval-millis", "100")

    //创建kafkasource
    val kafkaConsumer: FlinkKafkaConsumer[String] = new FlinkKafkaConsumer[String](
      java.util.regex.Pattern.compile("test-[0-9]"),
      new SimpleStringSchema(),
      properties)


    //    kafkaConsumer.setStartFromEarliest() // 尽可能从最早的记录开始
    kafkaConsumer.setStartFromLatest() // 从最新的记录开始
    //    kafkaConsumer.setStartFromTimestamp(...)  // 从指定的时间开始（毫秒）
    //    kafkaConsumer.setStartFromGroupOffsets()  // 默认的方法

    val kafkaStream: DataStream[String] = env.addSource(kafkaConsumer)

    kafkaStream.print()

    env.execute()

  }

}
